var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/filterRules/component/rulesController/index.tsx
var rulesController_exports = {};
__export(rulesController_exports, {
  RulesController: () => RulesController
});
module.exports = __toCommonJS(rulesController_exports);
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_util = require("../../../../util");
var import_types = require("../../types");
var import_index = require("./index.less");
var ITEM_HEIGHT = 32;
var MARGIN = 16;
var weakMap = /* @__PURE__ */ new WeakMap();
var RulesController = (props) => {
  const {
    value,
    disabled,
    maxLevel,
    maxSize,
    component,
    onAddCondition,
    onDeleteCondition,
    onChangeCondition,
    onChangeRowValues
  } = props;
  const isCondition = (item) => (item == null ? void 0 : item.type) && [import_types.ROW_PERMISSION_RELATION.AND, import_types.ROW_PERMISSION_RELATION.OR].includes(item == null ? void 0 : item.type);
  const calculateTreeItemHeight = (item, disabled2) => {
    var _a, _b;
    const composeDisabled = disabled2 || !!item.disabled;
    if (!(item == null ? void 0 : item.children))
      return weakMap.set(item, { height: ITEM_HEIGHT + MARGIN, lineHeight: ITEM_HEIGHT });
    item.children.map((child) => calculateTreeItemHeight(child, composeDisabled));
    const isLastCondition = !item.children.some(isCondition);
    const firstNodeIsCondition = isCondition(item.children[0]);
    if (!composeDisabled) {
      const height = item.children.reduce((prev, curr) => prev + weakMap.get(curr).height, ITEM_HEIGHT);
      let lineHeight;
      if (isLastCondition) {
        const firstNodeLineHeight = weakMap.get(item.children[0]).height - MARGIN;
        const lastNodeHeight = ITEM_HEIGHT;
        lineHeight = height - firstNodeLineHeight / 2 - lastNodeHeight / 2;
      } else {
        const firstNodeLineHeight = firstNodeIsCondition ? weakMap.get(item.children[0]).lineHeight / 2 + ITEM_HEIGHT / 2 : ITEM_HEIGHT / 2 + MARGIN;
        lineHeight = firstNodeLineHeight + ((_a = item.children) == null ? void 0 : _a.slice(1).reduce((prev, curr) => prev + weakMap.get(curr).height, ITEM_HEIGHT / 2));
      }
      weakMap.set(item, { height, lineHeight });
    } else {
      const height = item.children.reduce((prev, curr) => prev + weakMap.get(curr).height, 0);
      let lineHeight;
      let bottom;
      if (isLastCondition) {
        lineHeight = height - ITEM_HEIGHT - MARGIN;
      } else {
        const firstNode = item.children[0];
        const lastNode = item.children[item.children.length - 1];
        const firstNodeLineHeight = weakMap.get(firstNode).height - getNodeReduceHeight(item, true);
        const lastNodeIsCondition = isCondition(lastNode);
        const reduceLastHeight = getNodeReduceHeight(item, false);
        const lastNodeLineHeight = weakMap.get(lastNode).height - MARGIN - reduceLastHeight;
        bottom = lastNodeIsCondition ? reduceLastHeight : ITEM_HEIGHT / 2;
        lineHeight = firstNodeLineHeight + ((_b = item.children) == null ? void 0 : _b.slice(1, -1).reduce((prev, curr) => prev + weakMap.get(curr).height, 0)) + lastNodeLineHeight;
      }
      weakMap.set(item, { bottom, lineHeight, height });
    }
    return item;
  };
  const getNodeReduceHeight = (item, isFirst) => {
    var _a, _b, _c, _d;
    const currentNode = isFirst ? (_a = item == null ? void 0 : item.children) == null ? void 0 : _a[0] : (_c = item == null ? void 0 : item.children) == null ? void 0 : _c[((_b = item == null ? void 0 : item.children) == null ? void 0 : _b.length) - 1];
    if (!currentNode)
      return ITEM_HEIGHT / 2;
    const currentNodeIsCondition = isCondition(currentNode);
    if (currentNodeIsCondition) {
      return ((_d = weakMap.get(currentNode)) == null ? void 0 : _d.lineHeight) / 2 + getNodeReduceHeight(currentNode, isFirst);
    }
    return ITEM_HEIGHT / 2;
  };
  const renderCondition = (item, namePath, disabled2) => {
    var _a, _b;
    const composeDisabled = disabled2 || !!item.disabled;
    if ((_a = item == null ? void 0 : item.children) == null ? void 0 : _a.length) {
      const childrenPath = (index) => {
        const newPath = [...namePath];
        newPath.push(...["children", index]);
        return newPath;
      };
      const { lineHeight, bottom } = weakMap.get(item);
      return /* @__PURE__ */ React.createElement(
        "div",
        {
          key: item.key,
          className: import_util.iup.classNames("ruleController__condition", {
            "ruleController__condition--active": item.children.length > 1
          })
        },
        /* @__PURE__ */ React.createElement(
          "div",
          {
            className: import_util.iup.classNames("condition__box", {
              disabled: composeDisabled
            }),
            style: { height: lineHeight, bottom: bottom ?? MARGIN }
          },
          /* @__PURE__ */ React.createElement(
            import_antd.Radio.Group,
            {
              size: "small",
              className: "condition__box--radio",
              value: item == null ? void 0 : item.type,
              buttonStyle: "solid",
              onChange: (e) => !composeDisabled && onChangeCondition(item.key, item == null ? void 0 : item.type)
            },
            /* @__PURE__ */ React.createElement(import_antd.Radio.Button, { value: import_types.ROW_PERMISSION_RELATION["AND"] }, import_types.ROW_PERMISSION_RELATION_TEXT[import_types.ROW_PERMISSION_RELATION.AND]),
            /* @__PURE__ */ React.createElement(import_antd.Radio.Button, { value: import_types.ROW_PERMISSION_RELATION["OR"] }, import_types.ROW_PERMISSION_RELATION_TEXT[import_types.ROW_PERMISSION_RELATION.OR])
          ),
          item.children.length > 1 && /* @__PURE__ */ React.createElement(
            "span",
            {
              className: import_util.iup.classNames("condition__box--line", {
                disabled: disabled2
              })
            }
          )
        ),
        item.children.map(
          (d, index) => renderCondition(d, childrenPath(index), composeDisabled)
        ),
        !composeDisabled && /* @__PURE__ */ React.createElement("div", { className: "condition__add" }, /* @__PURE__ */ React.createElement("span", { className: "condition__add--line" }), /* @__PURE__ */ React.createElement(
          "span",
          {
            className: import_util.iup.classNames(
              "condition__add--icon",
              ((_b = item == null ? void 0 : item.children) == null ? void 0 : _b.length) >= maxSize ? "condition__add--icon--disabled" : ""
            ),
            onClick: () => {
              if ((item == null ? void 0 : item.children) && item.children.length >= maxSize)
                return;
              onAddCondition({
                key: item.key,
                isOut: true
              });
            }
          },
          "新增条件组"
        ))
      );
    }
    return /* @__PURE__ */ React.createElement("div", { className: "ruleController__item", key: item.key }, (value == null ? void 0 : value.children) && /* @__PURE__ */ React.createElement(
      "span",
      {
        className: import_util.iup.classNames("ruleController__item--line", {
          disabled: disabled2
        })
      }
    ), /* @__PURE__ */ React.createElement("div", { className: "ruleController__item--component" }, component({
      rowKey: item.key,
      disabled: composeDisabled,
      name: [...namePath, "rowValues"],
      rowValues: item.rowValues,
      onChange: onChangeRowValues
    })), !composeDisabled && /* @__PURE__ */ React.createElement("div", { className: "ruleController__item--operation" }, /* @__PURE__ */ React.createElement(import_icons.DeleteOutlined, { className: "icon", onClick: () => onDeleteCondition(item.key) }), item.level === maxLevel ? null : /* @__PURE__ */ React.createElement(import_icons.PlusSquareOutlined, { className: "icon", onClick: () => onAddCondition({ key: item.key }) })));
  };
  if (!value)
    return null;
  calculateTreeItemHeight(value, !!disabled);
  return /* @__PURE__ */ React.createElement("div", { className: "ruleController" }, renderCondition(value, [], !!disabled || !!value.disabled));
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RulesController
});
